﻿using HiMap.Carto;
using HiMap.Geometry;
using HiMap.MapControls;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;


namespace HiMap.MapControls.Tools
{
    

    public class ToolSelected : ToolClass
    {
        protected Button _bt3;
        protected Button _btExit;
        protected ComboBox _cmb;
        protected Form _frm;
        protected IFeatureLayer _layer;
        protected Panel _pl;
        private Image imgOld;
        protected bool isMouseDown;
        internal List<IFeature> list;
        protected int StaX;
        protected int StaY;

        public ToolSelected(Form frm, IFeatureLayer layer)
        {
            this._frm = frm;
            this._layer = layer;
            this.list = new List<IFeature>();
            this.CreateControls();
        }

        protected virtual void Btn3Click(object sender, EventArgs e)
        {
            if (this._cmb.SelectedIndex != -1)
            {
                IFeature feature = this.list[this._cmb.SelectedIndex];
                base.Map.SelectedFeature = feature;
                IGeometry geo = feature.Shape;
                base.Map.MapImage = (Image) this.imgOld.Clone();
                Graphics graphics = Graphics.FromImage(base.Map.MapImage);
                switch (geo.Type)
                {
                    case GeometryType.Point:
                    {
                        IPoint point = (IPoint) geo;
                        Point point2 = base.Map.MapToScreen(point.X, point.Y);
                        graphics.DrawRectangle(new Pen(Color.FromArgb(0x80, 0xff, 0xff)), new Rectangle(point2.X - 4, point2.Y - 4, 7, 7));
                        break;
                    }
                    case GeometryType.Polyline:
                        graphics.DrawLines(new Pen(Color.FromArgb(0x80, 0xff, 0xff)), base.Map.Map.MapToScreen(this.getPts(geo)));
                        break;

                    case GeometryType.Polygon:
                        graphics.DrawPolygon(new Pen(Color.FromArgb(0x80, 0xff, 0xff)), base.Map.Map.MapToScreen(this.getPts(geo)));
                        break;
                }
                this._pl.Visible = false;
                base.Map.picMap.Refresh();
            }
        }

        protected virtual void BtnExitClick(object sender, EventArgs e)
        {
            base.Map.SelectedFeature = null;
            this._pl.Visible = false;
            this._cmb.Items.Clear();
        }

        private void CmbSelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private void CreateControls()
        {
            this._pl = new Panel();
            this._bt3 = new Button();
            this._btExit = new Button();
            this._cmb = new ComboBox();
            this._pl.Width = 240;
            this._pl.Height = 0x1b;
            this._pl.BackColor = Color.FromArgb(0xc0, 0xc0, 0xc0);
            this._pl.MouseDown += new MouseEventHandler(this.panCtrl_MouseDown);
            this._pl.MouseMove += new MouseEventHandler(this.panCtrl_MouseMove);
            this._pl.MouseUp += new MouseEventHandler(this.panCtrl_MouseUp);
            this._pl.Visible = false;
            this._frm.Controls.Add(this._pl);
            this._frm.Controls.SetChildIndex(this._pl, 0);
            this._cmb.Location = new Point(3, 3);
            this._cmb.Width = 0x83;
            this._cmb.Height = 0x16;
            this._cmb.SelectedIndexChanged += new EventHandler(this.CmbSelectedIndexChanged);
            this._pl.Controls.Add(this._cmb);
            this._bt3.Text = "选中";
            this._bt3.Location = new Point(0x89, 3);
            this._bt3.Width = 0x2f;
            this._bt3.Height = 20;
            this._bt3.Click += new EventHandler(this.Btn3Click);
            this._pl.Controls.Add(this._bt3);
            this._btExit.Text = "取消";
            this._btExit.Location = new Point(190, 3);
            this._btExit.Width = 0x2f;
            this._btExit.Height = 20;
            this._btExit.Click += new EventHandler(this.BtnExitClick);
            this._pl.Controls.Add(this._btExit);
        }

        private IPoint[] getPts(IGeometry geo)
        {
            IPointCollection points = (IPointCollection) geo;
            IPoint[] pointArray = new PointClass[points.PointCount];
            for (int i = 0; i < points.PointCount; i++)
            {
                pointArray[i] = new PointClass();
                pointArray[i] = points.GetPoint(i);
            }
            return pointArray;
        }

        protected override void OnMouseDown(object sender, MouseEventArgs e)
        {
            IPoint point = new PointClass();
            point.X = e.X;
            point.Y = e.Y;
            IEnvelope envelope = base.Map.Map.Envelope;
            ISpatialFilter filter = new SpatialFilterClass();
            filter.Geometry = point;
            filter.Buffer = 20;
            IFeature feature = null;
            IFeatureCursor cursor = null;
            IFeatureLayer layer = null;
            this.list = new List<IFeature>();
            this._cmb.Items.Clear();
            for (int i = 0; i < base.Map.Map.LayerCount; i++)
            {
                layer = (IFeatureLayer) base.Map.Map.GetLayer(i);
                cursor = ((IFeatureClass) base.Map.Map.GetLayer(i).Class).Search(filter);
                cursor.Flush();
                for (feature = cursor.Next(); feature != null; feature = cursor.Next())
                {
                    this.list.Add(feature);
                    this._cmb.Items.Add("图层：" + layer.Caption + " 名称：" + feature.GetValue(0).ToString());
                }
            }
            if (((e.Y + this._pl.Height) + 5) > base.Map.Height)
            {
                this._pl.Location = new Point(this._pl.Location.X, e.Y - 5);
            }
            else
            {
                this._pl.Location = new Point(this._pl.Location.X, (e.Y + this._pl.Height) + 5);
            }
            if (this._cmb.Items.Count > 0)
            {
                this._cmb.SelectedIndex = 0;
            }
            this._pl.Visible = true;
        }

        protected override void OnToolCreate(MapControl Map)
        {
            base.OnToolCreate(Map);
            this.imgOld = (Image) base.Map.MapImage.Clone();
        }

        protected override void OnToolUpdated(object sender, EventArgs e)
        {
            this.imgOld.Dispose();
            base.Map.SelectedFeature = null;
            this._frm.Controls.Remove(this._bt3);
            this._frm.Controls.Remove(this._btExit);
            this._frm.Controls.Remove(this._cmb);
            this._frm.Controls.Remove(this._pl);
            this.BtnExitClick(null, null);
        }

        private void panCtrl_MouseDown(object sender, MouseEventArgs e)
        {
            this.StaX = e.X;
            this.StaY = e.Y;
            this.isMouseDown = true;
        }

        private void panCtrl_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.isMouseDown)
            {
                this._pl.Location = new Point(0, (this._pl.Location.Y + e.Y) - this.StaY);
            }
        }

        private void panCtrl_MouseUp(object sender, MouseEventArgs e)
        {
            this.isMouseDown = false;
        }
    }
}

